const app = getApp()

Page({
  data: {
    logo: '',
    name:'',
    imageSrc: "",
    ssid: '',
    pass: '',
    statue: '',
    shopName: '',
    id: '',
    telPhone: '',
    token: wx.getStorageSync('token') || '',
    showButton: false,
    iv: '',
    encryptedData: '',
    specialCode: '' // 解析手机号专用的code
  },
  onLoad: function (options) {
    this.setData({
      ssid: options.ssid,
      pass: options.pass,
      logo: options.logo,
      name: options.name,
      imageSrc: options.imageSrc,
      id: options.id,
      token: wx.getStorageSync('token') || '',
      showButton: false
    });
    this.queryPhone();
  },
  onShow: function () {
    let that = this
    app.fadein(that, 'current3', 0)
    app.sliderightshow(that, 'current3', -500, 0)
    
    setTimeout(function () {
      app.sliderightshow(that, 'current3', 0, 1)
    }, 500)
  },
  //页面隐藏时，触发渐出动画
  onHide: function () {
    let that = this
    app.fadein(that, 'current3', 1)
    app.sliderightshow(that, 'current3', 200, 1)
  },
  getPhoneNumber (e) {
    // console.log(e.detail.errMsg)
    // console.log(e.detail.iv)
    // console.log(e.detail.encryptedData)
    // console.log('获取手机号授权成功')
    this.setData({
      iv: e.detail.iv,
      encryptedData: e.detail.encryptedData
    });
    // 存手机号
    this.startWifi(this.data.ssid,this.data.pass);
  },
  startWifi: function (ssid, password) {
    var that = this;
    console.log("startWifi方法调用成功")
    wx.showLoading({
      title: '加载中',
    })
    wx.startWifi({
      success: function (res) {
        wx.getSystemInfo({
          success: (res)=> {
            console.log('wx.getSystemInfo调用成功',res.platform)
            if (res.platform == "ios") {   
              console.log('IOS');
            }
            if (res.platform == "android ") {  
              console.log('android') 
            }
          }
        })
        wx.connectWifi({
          SSID: ssid,
          password: password,
          success: function (res) {
            console.log('connectWifi成功', JSON.stringify(res));
            wx.getConnectedWifi({
              success: function (res) {
                if(ssid == res.wifi.SSID ){
                  that.setData({
                    statue:'已安全连接至店铺官方无线网络:',
                    shopName: that.data.name
                  })
                  that.successWifi();
                }
              },
            })
          },
          fail: function (res) {
            wx.hideLoading()
            console.log('errCode===='+res.errCode+'errMsg===='+res.errMsg);
            that.setData({
              statue:that.errorStr(res.errCode),
              shopName: ''
            })
            console.log('res',res)
            
            that.failWifi(res.errCode, that.errorStr(res.errCode));
          }
        })

      },
      fail: function (res) {
        wx.showToast({
          title: res.errMsg,
          icon: 'none',
          duration: 5000
        })
        wx.hideLoading()
      }
    })
  },
  errorStr: function (msg) {
    if (msg == "12001") return "当前系统不支持相关能力";
    if (msg == "12002") return "查不到账号，请联系系统管理员";
    if (msg == "12003") return "请确认是否在组织Wifi覆盖范围内";
    if (msg == "12004") return "重复连接Wifi";
    if (msg == "12005") return "未打开Wifi开关";
    if (msg == "12006") return "未打开GPS定位开关";
    if (msg == "12007") return "用户拒绝授权链接Wifi";
    if (msg == "12008") return "查不到账号，请联系系统管理员";
    if (msg == "12009") return "系统运行商配置拒绝链接Wifi";
    if (msg == "12010") return "未知错误，请联系系统管理员";
    if (msg == "12011") return "应用在后台无法配置Wifi";
    if (msg == "12013") return "查不到账号，请联系系统管理员";
    if (msg == "12014") return "";
  },
  queryPhone() { // 查询手机是否授权
    let that = this;
    wx.showLoading({
      title: '加载中',
    });
    wx.login({
      success: res => {
        wx.request({
          url: 'https://test.cloudcampusapps.top:8080/weChatWifiLogin/shopCustomer/queryPhone?code='+res.code,
          header:{
            "Content-Type": "applciation/text",
            "token": wx.getStorageSync('token') 
          },
          method:'GET',
          success:function(res){
            if(res.data.code == 200) {
              if(res.data.data && res.data.data.code == 10001){ // 没有注册过，
                wx.hideLoading()
                wx.login({
                  success: res => {
                    that.setData({
                      showButton: true,
                      specialCode: res.code
                    });
                  }
                })
              } else {//成功注册过，有授权的就是 
                that.startWifi(that.data.ssid,that.data.pass);
              }
            }
          },
          fail:function(err){
            console.log('err=============='+err)
          }
        })
      }
    })
   
  },
  successWifi() {
    let that = this
    wx.hideLoading();
    let params={
      shopId: that.data.id,
      iv: that.data.iv,
      encryptedData: that.data.encryptedData,
      code: that.data.specialCode
    };
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: 'https://test.cloudcampusapps.top:8080/weChatWifiLogin/shopCustomer/queryAuthorized',
      header: {
        "Content-Type": "application/json",
        "token": wx.getStorageSync('token') 
      },
      method: "POST",
      data: JSON.stringify(params),
      success:function(res){
        that.setData({
          specialCode: ''
        });
        
        wx.hideLoading();
      },
      fail:function(err){
        console.log('err=============='+err)
        wx.hideLoading()
      }
    })
   
  },
  failWifi(code,reason) {
    let that = this
    wx.hideLoading();
    let params={
      shopId: that.data.id,
      code: code,
      reason: reason
    };
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: 'https://test.cloudcampusapps.top:8080/weChatWifiLogin/shopCustomer/addConnectErrorInfo',
      header: {
        "Content-Type": "application/json",
        "token": wx.getStorageSync('token') 
      },
      method: "POST",
      data: JSON.stringify(params),
      success:function(res){
        
        wx.hideLoading();
      },
      fail:function(err){
        console.log('err=============='+err)
        wx.hideLoading()
      }
    })
   
  }

})
